import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * UserGroupManagement 用户组管理
 * @date: 2019-1-14


 */
import request from 'utils/request';
import { CPAAS_IAM } from 'utils/config';
import { getCurrentOrganizationId, isTenantRoleLevel, parseParameters } from 'utils/utils';
var tenantId = getCurrentOrganizationId();

function userGroup() {
  return isTenantRoleLevel() ? "".concat(tenantId, "/user-groups") : "user-groups";
}
/**
 * 删除用户组
 * @param {Object} params - 参数
 */


export function deleteUserGroup(_x) {
  return _deleteUserGroup.apply(this, arguments);
}
/**
 * 查询用户组
 * @param {Object} params - 查询参数
 */

function _deleteUserGroup() {
  _deleteUserGroup = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            return _context.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(userGroup()), {
              method: 'DELETE',
              body: params
            }));

          case 1:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _deleteUserGroup.apply(this, arguments);
}

export function fetchUserGroupList(_x2) {
  return _fetchUserGroupList.apply(this, arguments);
}
/**
 * 创建用户组
 * @param {Object} params - 参数
 */

function _fetchUserGroupList() {
  _fetchUserGroupList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(userGroup()), {
              method: 'GET',
              query: params
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _fetchUserGroupList.apply(this, arguments);
}

export function createUserGroup(_x3) {
  return _createUserGroup.apply(this, arguments);
}
/**
 * 修改用户组
 * @param {Object} params - 参数
 */

function _createUserGroup() {
  _createUserGroup = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(userGroup()), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _createUserGroup.apply(this, arguments);
}

export function updateUserGroup(_x4) {
  return _updateUserGroup.apply(this, arguments);
}
/**
 * 查询用户组明细
 * @param {Object} params - 查询参数
 */

function _updateUserGroup() {
  _updateUserGroup = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(userGroup()), {
              method: 'PUT',
              body: params
            }));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _updateUserGroup.apply(this, arguments);
}

export function getUserGroupDetail(_x5) {
  return _getUserGroupDetail.apply(this, arguments);
}
/**
 * 查询 剩余未分配用户组
 * @return {Promise<void>}
 */

function _getUserGroupDetail() {
  _getUserGroupDetail = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    var userGroupId;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            userGroupId = params.userGroupId;
            return _context5.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(userGroup(), "/details/").concat(userGroupId), {
              method: 'GET'
            }));

          case 2:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _getUserGroupDetail.apply(this, arguments);
}

export function getCurrentRestGroupUsers(_x6) {
  return _getCurrentRestGroupUsers.apply(this, arguments);
}
/**
 * 分配 用户给用户组
 * @return {Promise<void>}
 */

function _getCurrentRestGroupUsers() {
  _getCurrentRestGroupUsers = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(userGroupId) {
    var params,
        parsedParams,
        _args6 = arguments;
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            params = _args6.length > 1 && _args6[1] !== undefined ? _args6[1] : {};
            parsedParams = parseParameters(params);
            return _context6.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(userGroup(), "/").concat(userGroupId, "/users/exclude-users"), {
              method: 'GET',
              query: parsedParams
            }));

          case 3:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _getCurrentRestGroupUsers.apply(this, arguments);
}

export function assignUsersToGroup(_x7, _x8, _x9) {
  return _assignUsersToGroup.apply(this, arguments);
}
/**
 * 删除 已分配的用户
 * @return {Promise<void>}
 */

function _assignUsersToGroup() {
  _assignUsersToGroup = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(userGroupId, groupTenantId, body) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(userGroup(), "/").concat(userGroupId, "/users"), {
              method: 'POST',
              body: body,
              query: {
                tenantId: groupTenantId
              }
            }));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _assignUsersToGroup.apply(this, arguments);
}

export function delCurrentGroupUsers(_x10, _x11) {
  return _delCurrentGroupUsers.apply(this, arguments);
}
/**
 * 查询 已分配的用户组
 * @return {Promise<void>}
 */

function _delCurrentGroupUsers() {
  _delCurrentGroupUsers = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(userGroupId, body) {
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            return _context8.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(userGroup(), "/").concat(userGroupId, "/users"), {
              method: 'DELETE',
              body: body
            }));

          case 1:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _delCurrentGroupUsers.apply(this, arguments);
}

export function getCurrentGroupUsers(_x12, _x13) {
  return _getCurrentGroupUsers.apply(this, arguments);
}

function _getCurrentGroupUsers() {
  _getCurrentGroupUsers = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(userGroupId, params) {
    var parsedParams;
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            parsedParams = parseParameters(params);
            return _context9.abrupt("return", request("".concat(CPAAS_IAM, "/v1/").concat(userGroup(), "/").concat(userGroupId, "/users"), {
              method: 'GET',
              query: parsedParams
            }));

          case 2:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _getCurrentGroupUsers.apply(this, arguments);
}